var express = require('express');
var router = express.Router();
const  users = require('../controllers/users')
const uploadFile=require("../utils/upload");
const svgCaptcha = require('svg-captcha');

router.get('/', function(req, res, next) {
  var name='';thumb='';
  if(req.session){
    if(req.session.user)name = req.session.user;
    if(req.session.thumb)thumb="/upload/"+req.session.thumb
  }  
  res.render('users/index', {user:name,thumb:thumb});
});
router.get('/login', function(req, res, next) {
  res.render('users/login', {})
})
router.get('/regist', function(req, res, next) {
  res.render('users/regist', {})
})
router.get('/logout',function(req,res){   
  req.session.destroy(function(err){   
      console.log("退出登录！"+err);
      res.redirect('/users/')
  });
});
router.get('/captcha',(req, res)=>{
    const cap = svgCaptcha.create({          
            inverse: false,// 翻转颜色          
            fontSize: 36,// 字体大小          
            noise: 3,// 噪声线条数          
            width: 80,// 宽度          
            height: 30,// 高度
        });
    req.session.captcha = cap.text; // session 存储验证码数值
    res.type('svg'); // 响应的类型
    res.send(cap.data)
})
router.get('/download', async (req, res) => {
  let url = './public' + req.query['url']
  res.download(url)
})

router.post('/login', users.login)

router.post('/regist', uploadFile,users.regist)


module.exports = router;
